package com.difsonapps.contacttranslator.utils;

import java.io.File;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class Utils {

	static Logger logger = Logger.getLogger(Utils.class);
	public static boolean containt(String myString, String exp) {
		CharSequence cs = exp;
		boolean result = myString.contains(cs);
		return result;
	}

	public static List<String> splitToList(String myString, String exp) {
		List<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(myString, exp);
		int i = 0;
		while (st.hasMoreElements()) {
			result.add(st.nextToken());
		}
		return result;
	}

	public static String md5(String sInput) {

		String algorithm = "";
		if (sInput == null) {
			return "null";
		}
		try {
			algorithm = System.getProperty("MD5.algorithm", "MD5");
		} catch (SecurityException se) {
		}
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(algorithm);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		byte buffer[] = sInput.getBytes();

		for (int count = 0; count < sInput.length(); count++) {
			md.update(buffer, 0, count);
		}
		byte bDigest[] = md.digest();
		BigInteger bi = new BigInteger(bDigest);
		return (bi.toString(16));
	}

	public static String message(String key, Object[] args) {

		// process internationalisation
		return key;
	}


	public static String props(String key) {
		return key;
	}

	public static boolean validate(String hex) {

		Pattern pattern;
		Matcher matcher;
		pattern = Pattern.compile(Constant.EMAIL_PATTERN);
		matcher = pattern.matcher(hex);
		return matcher.matches();

	}
	
	/**
	 * A implementer
	 * @param length
	 * @return
	 */
	public static String unique(int length)
	{
		return null;
	}

	public  static FCMessage getMessage(String appli,String lang,String type, String messagesURl)
	{
	
		FCMessage fcMessage = null;
		FCRootMessage fcRootMessate= null;
		JAXBContext jcx;

		try {

			jcx = javax.xml.bind.JAXBContext.newInstance(FCRootMessage.class);
			
			
			Unmarshaller unmarshaller = jcx.createUnmarshaller();

			fcRootMessate = (FCRootMessage) unmarshaller.unmarshal(new File(messagesURl));
			if(fcRootMessate!=null)
				fcMessage = fcRootMessate.find(appli, lang, type);
			//logger.info("===> messages  "+fcMessage);
		} catch (Exception e) {
			
			logger.error(e.getMessage(),e);
			e.printStackTrace();
		}
          return fcMessage;
	}

}
